package com.ruoyi.project.system.service.impl;

import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.project.system.domain.SysDeptImg;
import com.ruoyi.project.system.domain.SysFood;
import com.ruoyi.project.system.domain.SysSpec;
import com.ruoyi.project.system.domain.SysType;
import com.ruoyi.project.system.es.SysTypeRepository;
import com.ruoyi.project.system.mapper.SysFoodMapper;
import com.ruoyi.project.system.mapper.SysSpecMapper;
import com.ruoyi.project.system.mapper.SysTypeMapper;
import com.ruoyi.project.system.service.ISysFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class SysFoodServiceImpl implements ISysFoodService{

    @Autowired
    private SysTypeRepository sysTypeRepository;

    @Autowired
    private SysFoodMapper sysFoodMapper;

    @Autowired
    private SysTypeMapper sysTypeMapper;

    @Autowired
    private SysSpecMapper sysSpecMapper;

    @Autowired
    private ServerConfig serverConfig;



    @Override
    @DataScope(deptAlias = "d")
    public List<SysFood> selectFoodList(SysFood food) {
        return sysFoodMapper.selectFoodList(food);
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<SysType> selectTypeList(SysFood food) {
        return sysTypeMapper.selectTypeList(food);
    }

    @Override
    @Transactional
    public int insertType(SysType type) {
        int flag=sysTypeMapper.insertType(type);
        updateEsType(type);
        return flag;
    }
    /**
     * 新增保存商品信息
     *
     * @param food 商品信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFood(SysFood food) {
        if(!food.getFileList().isEmpty()){
            for(SysDeptImg  sysDeptImg:food.getFileList()){
                food.setImg(sysDeptImg.getUrl().replace(serverConfig.getUrl(),""));
            }
        }
        int flag=sysFoodMapper.insertFood(food);
        insertFoodSprec(food);
        addESFood(food);
        return flag;
    }

    public void addESFood(SysFood food){
        try{
            Optional<SysType> type=sysTypeRepository.findById(Integer.valueOf(food.getTypeId()+""));
            SysType sysType=type.get();
            List<SysFood> list=sysType.getFoodList();
            if(null!=list&&!list.isEmpty()){
                sysType.getFoodList().add(food);
            }else {
                list=new ArrayList<>();
                list.add(food);
                sysType.setFoodList(list);
            }
            sysTypeRepository.index(type.get());
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public void updateESFood(SysFood food){
        try{
            Optional<SysType> type=sysTypeRepository.findById(Integer.valueOf(food.getTypeId()+""));
            List<SysFood> foods=type.get().getFoodList();
            if(null!=foods){
                Boolean flag=true;
                for (int i = 0;i<foods.size(); i++) {
                    if(foods.get(i).getFoodId()==food.getFoodId()){
                        flag=false;
                        foods.set(i,food);
                    }
                }
                if(flag){
                    type.get().getFoodList().add(food);
                }
            }else{
                List<SysFood> list=new ArrayList<>();
                list=new ArrayList<>();
                list.add(food);
                type.get().setFoodList(list);
            }

            sysTypeRepository.index(type.get());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public SysFood selectFoodById(Long foodId) {
        SysFood sysFood=sysFoodMapper.selectFoodById(foodId);
        if(null!=sysFood){
            List<SysDeptImg> sysDeptImgList=new ArrayList<>();
            SysDeptImg sysDeptImg=new SysDeptImg();
            sysDeptImg.setUrl(serverConfig.getUrl()+sysFood.getImg());
            sysDeptImgList.add(sysDeptImg);
            sysFood.setFileList(sysDeptImgList);
            sysFood.setSpecfoods(sysSpecMapper.selectSpecByFoodId(foodId));
        }
        return sysFood;
    }

    @Override
    @Transactional
    public int updateFood(SysFood food) {
        if(!food.getFileList().isEmpty()){
            for(SysDeptImg  sysDeptImg:food.getFileList()){
                food.setImg(sysDeptImg.getUrl().replace(serverConfig.getUrl(),""));
            }
        }
        int flag=sysFoodMapper.updateFood(food);
        sysSpecMapper.deleteByFoodId(food.getFoodId());
        insertFoodSprec(food);
        updateESFood(food);
        return flag;
    }


    public void insertFoodSprec(SysFood food)
    {
        List<SysSpec> list=food.getSpecfoods();
        for(SysSpec sysSpec:list){
            sysSpec.setFoodId(food.getFoodId());
            sysSpec.setCreateBy(food.getCreateBy());
            sysSpecMapper.insertSpec(sysSpec);
        }
    }

    public void updateEsType(SysType sysType){
       try{
           sysTypeRepository.index(sysType);
       }catch (Exception e){
           throw new RuntimeException(e);
       }

    }
}
